package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialRation;

/**材料费用额度管理
 * <p>Title: ServiceZMCM41.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 李红星
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-21 16:15:12  李红星  Created
 *</pre>
 */
public class ServiceZMCM41 extends ServiceEPBase{
	
	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM41</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21 16:17:52 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	/**得到上个月费用额度
	 * <p>Function getBeforeMonth</p>
	 * @param inInfo
	 * @param string
	 * @param name
	 *  <p>History:
	 *    2011-6-23 18:51:30 李红星
	 * </p>
	 */
	public EiInfo getBeforeMonth(EiInfo inInfo) {
		String fdDepartmentId=inInfo.get("fdDepartmentId").toString();//得到树节点id（机构id）
		StaticTools.setQueryParameters(inInfo, "fdDepartmentId", fdDepartmentId, true);//添加查询条件（单位id）
		EiInfo outInfo = super.query(inInfo,"ZMCMMaterialRation.querybeforemonth",new ZMCMMaterialRation());
		return outInfo;
	}

	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM41</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21 16:19:08 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		
//		String fdKeyId = inInfo.getString("fdKeyId");
		String fdDepartmentId=inInfo.getString("fdDepartmentId");//得到树节点id（机构id）
//		String deptName=inInfo.getString("deptName");       //得到树节点名称（机构名称）
		StaticTools.setQueryParameters(inInfo, "fdDepartmentId", fdDepartmentId, true);//添加查询条件		
		EiInfo outInfo = super.query(inInfo, "ZMCMMaterialRation.query",new ZMCMMaterialRation());
		return outInfo;
	}
	
	/**新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM41</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-31下午02:35:57 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验物资分类是否重复
		boolean validate = validate(inInfo, "ZMCMMaterialRation.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_MATERIALRATION"));//主键
			EiInfo outInfo = super.insert(inInfo, "ZMCMMaterialRation.insert");
			return outInfo;
		}
	}
	
	/**修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM41</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-23 16:21:08 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCMMaterialRation.update");
		return outInfo;
	}
	
	/**
	 *
	 * 校验重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-22下午02:35:57 李红星
	 * </p>
	 *
	 */	
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		String fdDepartmentId=inInfo.getBlock("result").getCellStr(0,"fdDepartmentId");//得到树节点id（机构id）
		String fdYear=inInfo.getBlock("result").getCellStr(0, "fdYear");
		String fdMonth=inInfo.getBlock("result").getCellStr(0, "fdMonth");
        params.put("fdDepartmentId", fdDepartmentId);
        params.put("fdYear", fdYear);
        params.put("fdMonth", fdMonth);
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("已存在该部门当前年月费用额度！");
			return true;
		}
		return false;
	}
}
